import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: () => import("../components/login/Login")
    },
    {
        path: '/home',
        component: ()=>import('../components/home/Home'),
        redirect: '/welcome',
        //子路由
        children: [
            { path:'/welcome', component: ()=>import('../components/home/Welcome')},
            { path:'/goods', component: ()=>import('../components/goods/Goods')},
            { path:'/goods/add', component: ()=>import('../components/goods/Add')},
            { path:'/users', component: ()=>import('../components/user/User')},
            { path:'/params', component: ()=>import('../components/goods/Params')},
            { path:'/categories', component: ()=>import('../components/goods/Cate')},
            { path:'/orders', component: ()=>import('../components/order/Order')},
            { path:'/reports', component: ()=>import('../components/report/Report')},
            { path:'/roles', component: ()=>import('../components/power/Roles')},
            { path:'/rights', component: ()=>import('../components/power/Rights')},
            { path:'/err/401', component: ()=>import('../components/err/401')},
            { path:'/err/404', component: ()=>import('../components/err/404')},
            { path:'/map/show', component: ()=>import('../components/map/Map')},
            { path:'/person/blog', component: ()=>import('../components/person/Blog')},
            { path:'/person/code', component: ()=>import('../components/person/Code')},
            { path:'/system/desc', component: ()=>import('../components/system/System')},
            { path:'/docs', component: ()=>import('../components/docs/Doc')}
        ]
    }
]

const router = new VueRouter({
    routes
})

//挂载路由导航守卫
router.beforeEach((to,form,next)=>{
    //to 将要访问的路径
    //from 代表从那个路径跳转而来
    //next表示一个函数代表放行
    if(to.path == '/login'){
        return next();
    }
    //获取token
    const token = window.sessionStorage.getItem("token");
    if(!token){
        next('/login');
    }
    next();
});
export default router
